--获取进度条

-- 引入Unity的命名空间
local UnityEngine = CS.UnityEngine
local GameObject = UnityEngine.GameObject
local Slider = UnityEngine.UI.Slider
local WaitForSeconds = UnityEngine.WaitForSeconds

-- 查找Canvas和Slider
local canvas = GameObject.Find("Canvas")
if canvas == nil then
    print("未找到Canvas")
    return
end

local panelObj = canvas.transform:Find("Panel")
if panelObj == nil then
    print("未找到Panel")
    return
end

local sliderObj = panelObj.transform:Find("Slider")
if sliderObj == nil then
    print("未找到Slider")
    return
end

-- 获取Slider组件
local slider = sliderObj:GetComponent(typeof(Slider))
if slider == nil then
    print("未找到Slider组件")
    return
end

-- 定义协程函数，实现进度条逐渐增加
local function ProgressBarCoroutine(duration)
    local time = 0
    slider.value = 0 -- 初始值为0

    while time < duration do
        time = time + UnityEngine.Time.deltaTime
        slider.value = time / duration -- 根据时间计算当前进度
        print("当前进度: " .. time / duration .. " 总时间: " .. duration .. " 当前时间: " .. time)
        coroutine.yield(time) -- 等待下一帧
    end

    slider.value = 1 -- 确保最终值为1
    print("进度条完成！")
end


-- 定义一个协程函数
--local function ProgressBarCoroutine(duration)
--    local time = 0
--    while time < duration do
--        time = time + UnityEngine.Time.deltaTime
--        print("当前进度: " .. time / duration)
--        coroutine.yield(time) -- 等待下一帧
--    end
--    print("进度条完成！")
--end

-- 启动协程
--local co = coroutine.start(ProgressBarCoroutine, 5) -- 5秒内完成


-- 创建协程
local co = coroutine.create(ProgressBarCoroutine)

for i = 1, 500 do
    
    
-- 启动协程
coroutine.resume(co, 5) -- 传入参数5（duration）
end

--设置进度条当前值